#name : fun binary exact ci by simulation
# key : bin.exact.ci.simulation.func
# contributor: Shuguang Sun
# --
${1:binary_exact} <- function(N = 20, true_p = 0.5,
                         target_p = 0.35, LCL_boundary = target_p, alpha = 0.05,
                         R = 1000, seed = NULL) {
  if (!is.null(seed) & is.numeric(seed)) {
    set.seed(seed)
  }

  r1 <- rbinom(R, N, true_p)
  f1 <- (r1 / N >= target_p)

  exactlo <- qbeta(alpha / 2, r1, N - r1 + 1)
  #  exacthi = qbeta(1 - alpha/2, r1 + 1, N - r1)
  f2 <- (exactlo > LCL_boundary)

  list(prob_ge_target = sum(f1)/R,
       prob_lcl_gt_boundary = sum(f2)/R)
}

$1($0)